package com.gykfbxxh.imovie.view.user.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.app.utils.util.ToastUtils;
import com.google.gson.reflect.TypeToken;
import com.gykfbxxh.imovie.R;
import com.gykfbxxh.imovie.base.BaseActivity;
import com.gykfbxxh.imovie.bean.RanNumBean;
import com.gykfbxxh.imovie.bean.info.UserInfor;
import com.gykfbxxh.imovie.config.URIContant;
import com.gykfbxxh.imovie.custom.net.ParamMap;
import com.gykfbxxh.imovie.listener.HttpListener;
import com.gykfbxxh.imovie.manager.UserInforManager;
import com.gykfbxxh.imovie.utils.HttpUtils;

public class UpdateUserNameActivity extends BaseActivity {
    private EditText et_username;
    private TextView tv_button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addContentView(R.layout.activity_update_user_name);
        setTitle("修改用户昵称");

        et_username = (EditText)findViewById(R.id.et_username);
        tv_button = (TextView)findViewById(R.id.tv_button);
        tv_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateData();
            }
        });

    }

    private void updateData() {
        if(et_username==null) {
            return;
        }
        final String userStr = et_username.getText().toString().trim();
        if(TextUtils.isEmpty(userStr)) {
            ToastUtils.makeText("新昵称不能为空,请输入!");
            return;
        }
        ParamMap paramMap = new ParamMap();
        paramMap.put("userName",userStr);
        HttpUtils.addPostRequest(URIContant.UPDATEUSERNAME, new TypeToken<RanNumBean>() {
        }.getType(), mPageName, paramMap, new HttpListener<RanNumBean>() {
            @Override
            public void onError(VolleyError error) {
                ToastUtils.makeText("修改失败");
            }

            @Override
            public void onSuccess(RanNumBean response) {
                if(response!=null&&response.noError()) {
                    ToastUtils.makeText("修改成功");
                    UserInfor userInfor = UserInforManager.getInstance().getUserInfor();
                    userInfor.setfUsername(userStr);
                    UserInforManager.getInstance().setUserInfor(userInfor);
                    Intent intent = new Intent();
                    setResult(RESULT_OK,intent);
                    onBackPressed();
                }else {
                    ToastUtils.makeText(response.getErrorMsg());
                }
            }
        });

    }
}
